package com.future.bantang.activity;

import android.os.PersistableBundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;


import com.future.bantang.R;
import com.future.bantang.adapter.TbsFraViewPagerAdapter;
import com.future.bantang.constants.BanTang;
import com.future.bantang.constants.URL;
import com.future.bantang.entity.GoodThingsCategorysEntity;
import com.future.bantang.utils.JsonUtils;
import com.future.bantang.utils.L;
import com.future.bantang.utils.NetUtils;
import com.future.bantang.view.PagerSlidingTabStrip;
import com.future.bantang.view.TitleBarView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * 好物
 */

@ContentView(R.layout.activity_good_things)
public class GoodThingsActivity extends BaseActivity {


    public static final String TAG = "GoodThingsActivity";
    //请求分类信息数据的地址
    private String url;

    //指示器
    @ViewInject(R.id.tabs_goodthings_act_top)
    private PagerSlidingTabStrip tabStrip;

    //
    @ViewInject(R.id.pager_goodthings_act_content)
    private ViewPager pager;

    @ViewInject(R.id.titleBar_goodthings_act_top)
    TitleBarView titleBar;


    private TbsFraViewPagerAdapter pagerAdapter;


    protected void init() {
        super.init();
        titleBar.getmIco().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GoodThingsActivity.this.finish();
            }
        });


        url= URL.CATEGORY_LIST_URL;
        pagerAdapter = new TbsFraViewPagerAdapter(getSupportFragmentManager());



    }


    protected void loadDatas() {
        super.loadDatas();
        //获取栏目分类信息
        NetUtils.RequestParamsBuilder builder=new NetUtils.RequestParamsBuilder();

        builder.addPostParameter(BanTang.CLIENT_ID_KEY, BanTang.CLIENT_ID_VALUE)
                .addPostParameter(BanTang.CLIENT_SECRET_KEY, BanTang.CLIENT_SECRET_VALUE);

        RequestParams requestParams = builder.build();

        NetUtils.doPost(url, requestParams, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    GoodThingsCategorysEntity categorysEntity = JsonUtils.perse(responseInfo.result, GoodThingsCategorysEntity.class);
                    List<GoodThingsCategorysEntity.DataEntity> dataEntities = categorysEntity.getData();


                    if (dataEntities.size()>0)
                    {
                        //添加进adapter
                        pagerAdapter.setCategorys(dataEntities);

                        //设置适配器
                        pager.setAdapter(pagerAdapter);
                        //绑定指示器与viewpager
                        tabStrip.setViewPager(pager);

                    }


                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Toast.makeText(GoodThingsActivity.this, "没有获取到数据，请检查网络！", Toast.LENGTH_SHORT).show();
            }
        });


    }
}
